package com.huifaapp.mall.selectImage;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.huifaapp.mall.R;

import java.util.ArrayList;

public class SelectAdapter extends BaseAdapter {
    private String[] title;
    Activity activity;
    ArrayList<String> titles;
    private boolean hasCancle;

    int color;

    public SelectAdapter(Activity activity, String[] titles) {
        this.activity = activity;
        this.title = titles;
        init(false);
    }

    public SelectAdapter(Activity activity, ArrayList<String> titles) {
        this.activity = activity;
        this.titles = new ArrayList<String>();
        this.titles.addAll(titles);
    }

    public SelectAdapter(Activity activity, String[] titles, boolean isCancle) {
        this(activity, titles);
        hasCancle = isCancle;
        init(isCancle);
    }

    public SelectAdapter(Activity activity, ArrayList<String> titles, boolean isCancle) {
        this(activity, titles);
        hasCancle = isCancle;
        if(isCancle){
            this.titles.add("取消");
        }
    }

    public void addItem(String item){
        if(hasCancle){
            titles.add(titles.size() - 1, item);
        }else{
            titles.add(item);
        }
        notifyDataSetChanged();
    }

    public String getItemText(int position){
        return titles.get(position);
    }

    public void removeItem(int index){
        titles.remove(index);
        notifyDataSetChanged();
    }

    public int getItemCount(){
        if(hasCancle){
            return titles.size() - 1;
        }else{
            return titles.size();
        }
    }

    private void init(boolean isCancle){
        titles = new ArrayList<String>();
        for (int i = 0; i < title.length; i++) {
            titles.add(title[i]);
        }
        if (isCancle) {
            titles.add("取消");
        }
    }

    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public String getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setTextColor(int color) {
        this.color = color;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        TextView text = null;
        if (position == (titles.size() - 1)) {
            view = LayoutInflater.from(activity).inflate(R.layout.select_atten_item_last, null);
            text = (TextView)view.findViewById(R.id.attenDesTitle);
            text.setTextColor(activity.getResources().getColor(R.color.text_deep_content));
        } else {
            view = LayoutInflater.from(activity).inflate(R.layout.select_atten_item, null);
            text = (TextView)view.findViewById(R.id.attenDesTitle);
            if (color != 0) {
                text.setTextColor(color);
            } else {
                text.setTextColor(activity.getResources().getColor(R.color.text_deep_content));
            }
        }

        text.setText(titles.get(position));
        return view;
    }
}
